using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DesignerSupport;

namespace XMTAEditor
{
	public partial class PropertyEditor : BaseEditor
	{
		public PropertyEditor()
		{
			InitializeComponent();
			editorBrowsable.EnumType = typeof(EditorBrowsableState);
			designerSerializationVisibility.EnumType = typeof(DesignerSerializationVisibility);
			refreshProperties.EnumType = typeof(RefreshProperties);
			checkBoxList1.Items.Add("Browsable", null, "Specifies whether a property or event should be displayed in a Properties window.");
			checkBoxList1.Items.Add("Supported", null, "???");
			checkBoxList1.Items.Add("Bindable", null, "Specifies whether a member is typically used for binding.");
			checkBoxList1.Items.Add("DesignOnly", null, "Specifies whether a property can only be set at design time.");
			checkBoxList1.Items.Add("ListBindable", null, "Specifies that a list can be used as a data source.");
			checkBoxList1.Items.Add("Localizable", null, "Specifies whether a property should be localized.");
			checkBoxList1.Items.Add("MergableProperty", null, "Specifies that this property can be combined with properties belonging to other objects in a Properties window.");
			checkBoxList1.Items.Add("NotifyParentProperty", null, "Indicates that the parent property is notified when the value of the property that this attribute is applied to is modified.");
			checkBoxList1.Items.Add("ParenthesizeProperty", null, "Indicates whether the name of the associated property is displayed with parentheses in the Properties window.");
			checkBoxList1.Items.Add("ReadOnly", null, "Specifies whether the property this attribute is bound to is read-only or read/write at design time.");
			checkBoxList1.Items.Add("SupressFiltering", null, "???");
		}

		protected PropertyType prop;

		public PropertyType Property
		{
			get { return prop; }
			set
			{
				prop = value;
				Fill();
			}
		}

		protected override void Fill()
		{
			description.Text = BindUtil.ToText(prop.Description);
			category.Text = BindUtil.ToText(prop.Category);
			editorBrowsable.EnumVal = BindUtil.ToText(prop.EditorBrowsable);
			designerSerializationVisibility.EnumVal = BindUtil.ToText(prop.DesignerSerializationVisibility);
			refreshProperties.EnumVal = BindUtil.ToText(prop.RefreshProperties);
			defaultValue.Text = BindUtil.ToText(prop.DefaultValue);
			ambientValue.Text = BindUtil.ToText(prop.AmbientValue);
			editor.Text = BindUtil.ToText(prop.Editor);
			typeConverter.Text = BindUtil.ToText(prop.TypeConverter);
			relatedImageList.Text = BindUtil.ToText(prop.RelatedImageList);
			checkBoxList1.Items[0].State = BindUtil.ToCheck(prop.Browsable);
			checkBoxList1.Items[1].State = BindUtil.ToCheck(prop.Supported);
			checkBoxList1.Items[2].State = BindUtil.ToCheck(prop.Bindable);
			checkBoxList1.Items[3].State = BindUtil.ToCheck(prop.DesignOnly);
			checkBoxList1.Items[4].State = BindUtil.ToCheck(prop.ListBindable);
			checkBoxList1.Items[5].State = BindUtil.ToCheck(prop.Localizable);
			checkBoxList1.Items[6].State = BindUtil.ToCheck(prop.MergableProperty);
			checkBoxList1.Items[7].State = BindUtil.ToCheck(prop.NotifyParentProperty);
			checkBoxList1.Items[8].State = BindUtil.ToCheck(prop.ParenthesizeProperty);
			checkBoxList1.Items[9].State = BindUtil.ToCheck(prop.ReadOnly);
			checkBoxList1.Items[10].State = BindUtil.ToCheck(prop.SuppressFiltering);
			checkBoxList1.Refresh();
		}

		public override void Save()
		{
			changed = false;
			prop.Description = BindUtil.ToVar(description.Text);
			prop.Category = BindUtil.ToVar(category.Text);
			prop.EditorBrowsable = BindUtil.ToVar(editorBrowsable.EnumVal);

			prop.DesignerSerializationVisibility = BindUtil.ToVar(designerSerializationVisibility.EnumVal);
			prop.RefreshProperties = BindUtil.ToVar(refreshProperties.EnumVal);
			prop.DefaultValue = BindUtil.ToObject(defaultValue.Text, GetPropertyType());
			prop.AmbientValue = BindUtil.ToObject(ambientValue.Text, GetPropertyType());
			prop.Editor = typedeftype.FromString(editor.Text);
			prop.TypeConverter = typedeftype.FromString(typeConverter.Text);
			prop.RelatedImageList = BindUtil.ToVar(relatedImageList.Text);
			prop.Browsable = BindUtil.FromCheck(checkBoxList1.Items[0].State);
			prop.Supported = BindUtil.FromCheck(checkBoxList1.Items[1].State);
			prop.Bindable = BindUtil.FromCheck(checkBoxList1.Items[2].State);
			prop.DesignOnly = BindUtil.FromCheck(checkBoxList1.Items[3].State);
			prop.ListBindable = BindUtil.FromCheck(checkBoxList1.Items[4].State);
			prop.Localizable = BindUtil.FromCheck(checkBoxList1.Items[5].State);
			prop.MergableProperty = BindUtil.FromCheck(checkBoxList1.Items[6].State);
			prop.NotifyParentProperty = BindUtil.FromCheck(checkBoxList1.Items[7].State);
			prop.ParenthesizeProperty = BindUtil.FromCheck(checkBoxList1.Items[8].State);
			prop.ReadOnly = BindUtil.FromCheck(checkBoxList1.Items[9].State);
			prop.SuppressFiltering = BindUtil.FromCheck(checkBoxList1.Items[10].State);
		}

		private Type GetPropertyType()
		{
			if (prop.DefaultValue != null && prop.DefaultValue.InternalType != null)
				return prop.DefaultValue.InternalType;
			return null;
		}

		private void label2_Click(object sender, EventArgs e)
		{

		}
	}
}

